package com.dc.dbus.transport.netty.server;

import com.dc.dbus.transport.commons.ServerSideReceiver;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;

public class MainHandlerInitializer extends ChannelInitializer<SocketChannel> {

	protected final ServerSideReceiver serverSideReceiver;
	
	protected final ClientChannelCenter clientChannelCenter;
	
	protected MainHandlerInitializer(ServerSideReceiver serverSideReceiver,
			ClientChannelCenter clientChannelCenter) {
		this.serverSideReceiver = serverSideReceiver;
		this.clientChannelCenter = clientChannelCenter;
	}
	
	@Override
    protected void initChannel(SocketChannel ch) throws Exception {
		
        ChannelPipeline pipeline = ch.pipeline();
        
        pipeline.addLast("encode", new ObjectEncoder());
        pipeline.addLast("decode", new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
        
        //主处理器
        MainHandler mainHandler = new MainHandler(clientChannelCenter, serverSideReceiver);
        pipeline.addLast("handler", mainHandler);
        
    }

}
